package main

import (
	"context"
	"gitee.com/shijun123456/kcgin"
	"gitee.com/shijun123456/srpcinterface/client/utils"
	"gitee.com/shijun123456/srpcinterface/client/votes"
	"google.golang.org/grpc"
	"log"
	"net"
	"os"
	"os/signal"
	"syscall"
)

type VoteImpl struct {
}

//其实都是从下载对应的 私有仓库 进行更新操作
//都是这样的处理方式 都是引用 srpc筒体个地方进行更新和成处理方式
//统一进行更新
//  golang-go rpc go rpc go rpc -> nodejs php go 版本的rpc信息
//  都是用的是一份信息
//  同步更新发布
func (v VoteImpl) GetInfo(ctx context.Context, info *votes.ArgsGetPayInfo) (*votes.ReplyGetPayinfo, error) {
	return &votes.ReplyGetPayinfo{
		Data: &votes.Pay{
			OrderNo:              info.OrderNo,
			PayTime:              "",
			PayStatus:            "",
			BusId:                "",
			CarId:                "",
			Txamt:                "",
			OldTxamt:             "",
			XXX_NoUnkeyedLiteral: struct{}{},
			XXX_unrecognized:     nil,
			XXX_sizecache:        0,
			Version:              "1.0",
		},
		Res: &votes.Response{
			Code: "200",
			Message: "OK",
		},
		Version: "1.0",
	}, nil
}

//服务注册
func register(client *utils.ConsulClient) {
	err := client.ServiceRegisterByMicro()
	if err != nil {
		log.Printf("err:%s", err.Error())
	}
}
func unregister(client *utils.ConsulClient) {
	client.ServiceUnRegisterByMicro()
}

var client *utils.ConsulClient = utils.DefaultConsulClient()

func main() {
	defer func() {
		if err := recover(); err != nil {
			unregister(client)
		}
	}()
	lis, err := net.Listen("tcp", kcgin.AppConfig.String("node.address"))
	if err != nil {
		log.Fatalf("err: %s", err)
	}

	server := grpc.NewServer()

	votes.RegisterPaySvcServer(server, &VoteImpl{})

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGALRM, os.Interrupt, syscall.SIGKILL)

	go register(client)
	go func() {
		<-ch
		unregister(client)
		os.Exit(1)
	}()

	log.Fatal(server.Serve(lis))

}
